package main

import (
	"database/sql"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
	"net/http"
	"strings"
	"twcdb/middleware"
)
var db *sql.DB

func sqlOpen() {
	var err error
	db, err = sql.Open("postgres", "port=5432 user=postgres password=123 dbname=postgres sslmode=disable")
	//port是数据库的端口号，默认是5432，如果改了，这里一定要自定义；
	//user就是你数据库的登录帐号;
	//dbname就是你在数据库里面建立的数据库的名字;
	//sslmode就是安全验证模式;

	//还可以是这种方式打开
	//db, err := sql.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full")
	checkErr(err)
	println("连接成功")
}
func main() {

	//db :=InitDB()
	//defer db.Close()
	r := gin.Default()
	r.Use(middleware.CORSMiddleware())
	r.POST("/ping", func(ctx *gin.Context) {

		//opreration :=ctx.PostForm("operation")
		eno :=ctx.PostForm("eno")
		ename :=ctx.PostForm("ename")
		esex :=ctx.PostForm("esex")
		ebirth :=ctx.PostForm("ebirth")
		ejoin :=ctx.PostForm("ejoin")
		setion :=ctx.PostForm("setion")
		ekind :=ctx.PostForm("ekind")
		tkind :=ctx.PostForm("tkind")
		jobtitle :=ctx.PostForm("jobtitle")
		ec :=ctx.PostForm("ec")
		nation :=ctx.PostForm("nation")
		addr :=ctx.PostForm("addr")
		naddr :=ctx.PostForm("naddr")
		ctx.Request.ParseForm()
		for k:= range ctx.Request.PostForm{
			fmt.Println("                            ")
			//fmt.Printf(k)
			s1 :=k[14: 19]
			s2 :=k[28: 38]
			//s3 :=k[46: 56]
			//fmt.Println(s1,s2)
			if s1=="selec" {
				eno,ename,esex,ebirth,ejoin,setion,ekind,tkind,jobtitle,ec,nation,addr,naddr=sqlSelect(s2)
				ctx.JSON(http.StatusOK,gin.H{"eno":eno,"ename":ename,"esex":esex,"ebirth":ebirth,"ejoin":ejoin,"setion":setion,"ekind":ekind,"tkind":tkind,"jobtitle":jobtitle,"ec":ec,"nation":nation,"addr": addr,"naddr":naddr})
			}
			if s1=="delet"{
				sqlDelete(s2)
			}
			if len(k)>100{
				desStr1 := strings.Split(k, ":")
				str1 :=desStr1[1][1:len(desStr1[1])-18]

				str2 :=desStr1[2][1:len(desStr1[2])-10]
				str3 :=desStr1[3][1:len(desStr1[3])-13]
				str4 :=desStr1[4][1:len(desStr1[4])-10]
				str5 :=desStr1[5][1:len(desStr1[5])-12]
				str6 :=desStr1[6][1:len(desStr1[6])-10]//no
				str7 :=desStr1[7][1:len(desStr1[7])-8] //num
				str8 :=desStr1[8][1:len(desStr1[8])-14]
				str9 :=desStr1[9][1:len(desStr1[9])-14]
				str10 :=desStr1[10][1:len(desStr1[10])-15]
				str11 :=desStr1[11][1:len(desStr1[11])-15]
				str12 :=desStr1[12][1:len(desStr1[12])-15]
				str13 :=desStr1[13][1:len(desStr1[13])-10]
			
				fmt.Println(str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,)
				sqlUpdate(str7,str1,str2,str3,str4,str5,str6,str8,str9,str10,str11,str12,str13)
				fmt.Println(len(str7))
			}
		}

		//sqlClose()
	})
	panic(r.Run(":8086") )// listen and serve on 0.0.0.0:8181

}
func sqlInsert(eno,ename,esex,ebirth,ejoin,setion,ekind,tkind,jobtitle,ec,nation,addr,naddr string) {
	//插入数据
	sqlOpen()
	stmt, err := db.Prepare("INSERT INTO employees(eno,ename,esex,ebirth,ejoin,setion,ekind,tkind,jobtitle,ec,nation,addr,naddr) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13) ")
	checkErr(err)

	res, err := stmt.Exec(eno,ename,esex,ebirth,ejoin,setion,ekind,tkind,jobtitle,ec,nation,addr,naddr)
	//这里的三个参数就是对应上面的$1,$2,$3了

	checkErr(err)

	affect, err := res.RowsAffected()
	checkErr(err)

	fmt.Println("rows affect:", affect)
   fmt.Println("success")
}
func sqlSelect(enos string) (eno,ename,esex ,ebirth, ejoin, setion, ekind, tkind, jobtitle, ec ,nation, addr, naddr string){
	//查询数据
	sqlOpen()
	rows, err := db.Query("SELECT * FROM employees")
	checkErr(err)

	println("-----------")
	for rows.Next() {
		var eno string
		var ename string
		var esex string
		var ebirth string
		var ejoin string
		var setion string
		var ekind string
		var tkind string
		var jobtitle string
		var ec string
		var nation string
		var addr string
		var naddr string

		err = rows.Scan(&eno, &ename, &esex, &ebirth, &ejoin, &setion, &ekind, &tkind, &jobtitle, &ec ,&nation, &addr, &naddr)
		println(len(eno),len(enos))
		if eno==enos{
			fmt.Println("here")
			checkErr(err)
			fmt.Println(eno,ename)

			return eno,ename,esex ,ebirth, ejoin, setion, ekind, tkind, jobtitle, ec ,nation, addr, naddr
		}


	}
	return "0","0","0","0","0","0","0","0","0","0","0","0","0"
}
func sqlUpdate(eno,addr,ebirth,ec,ejoin,ekind,ename,esex,tkind,naddr,nation,setion,jobtitle string) {
	//更新数据
	//sqlOpen()
	fmt.Println(len(addr),len(ebirth),len(ec),len(ejoin),len(ename),len(esex),len(tkind),len(naddr),len(nation),len(setion),len(jobtitle))
	stmt, err := db.Prepare("update employees set addr=$1 where eno=$3")
	checkErr(err)
	res, err := stmt.Exec(addr,eno)
	//res, err := stmt.Exec("ficow", 1)
	checkErr(err)

	affect, err := res.RowsAffected()
	checkErr(err)

	fmt.Println("rows affect:", affect)
}
func sqlDelete(eno string) {
	//删除数据
	sqlOpen()
	stmt, err := db.Prepare("delete from employees where eno=$1")
	checkErr(err)
	res, err := stmt.Exec(eno)

	checkErr(err)

	affect, err := res.RowsAffected()
	checkErr(err)

	fmt.Println("rows affect:", affect)
}
func sqlClose() {
	db.Close()
}
func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}